home *** CD-ROM | disk | FTP | other *** search
/ Super CD 5 / Super CD 5 (Groupware).bin / program / parser.bas < prev    next >
BASIC Source File  |  1994-05-03  |  7KB  |  158 lines

  1. ' ╔══════════════════════════════════════════════════╗
  2. ' ║   Main Program Name : Parser.Bas                 ║╗
  3. ' ║   Last Update       : 04/12/1994                 ║║
  4. ' ║   Compiled with PowerBasic vers  3.00c           ║║
  5. ' ║                                                  ║║
  6. ' ║   Uploaded by:                                   ║║
  7. ' ║   ============                                   ║║
  8. ' ║   Scott Tucker                                   ║║
  9. ' ║   Creative Software Alliance                     ║║
  10. ' ║   16 Lakeville Ave.                              ║║
  11. ' ║   Petaluma, CA  94952                            ║║
  12. ' ║   (707) 778-1591                                 ║║
  13. ' ║                                                  ║║
  14. ' ╚══════════════════════════════════════════════════╝║
  15. '  ╚══════════════════════════════════════════════════╝
  16. Cls
  17. ReDim A$(0)
  18.  
  19. ' Example #1
  20. '===========
  21. 'T$ = "This is a sample line of some words."
  22. 'Call Parser(T$, " ", A$(), 2)
  23.  
  24. ' Example #2
  25. '===========
  26. 'T$ = "This is a sample line of some words."+Chr$(13)+_
  27. '      "It contains 2 sentences to parse apart."
  28. 'Call Parser(T$, Chr$(13), A$(), 2)
  29.  
  30. ' Example #3
  31. '===========
  32. T$ = "\mn /tp /op /t:rew /lfre /abcdefg -pdq -dA:filename"
  33. Call Parser(T$, "/-\", A$(), 1)
  34.  
  35. ' Print items returned from procedure.
  36. For K%=1 to UBound(A$)
  37.   Print K%; "*" + RTrim$(A$(K%)) + "*"
  38. Next
  39.  
  40. End
  41. '========================================
  42. Sub Parser(ByVal Text$, ByVal Delimiter$, Parms$(), ByVal Flag%)
  43.   ' This procedure parses text based on a given set of delimiters.
  44.   ' The delimiter can either separate the items in the string, or
  45.   ' it can precede items in the string. The resulting parsed items
  46.   ' are returned in the array PARMS$().
  47.  
  48.   ' Parameter Descriptions
  49.   ' ======================
  50.   ' Text$      = This is the string of text containing the delimiters to
  51.   '              parse.
  52.   ' Delimiters$= This is the list of delimiters used to parse the string.
  53.   '              These delimiters can be any single string item, or a
  54.   '              multiple list of items. Examples: "/" or "/\-"
  55.   ' Parms$()   = The array that contains each item parsed out of the string.
  56.   '              This array is redimensioned (from 1) to hold the proper
  57.   '              number of parsed items. It should initially be Dim'ed to
  58.   '              Parms$(0) before being passed into this procedure. If no
  59.   '              delimiters are found in the string, the array will have a
  60.   '              dim'ed index of (0) and a null value is returned
  61.   '              in the array.
  62.   ' Flag%      = A flag indicating whether the delimiters separate the
  63.   '              string, or precede delimited items.
  64.   '              1 = Precede      2 = Separate
  65.  
  66.   '              The difference is defined as follows:
  67.   '    Type 1    A type 1 delimiter means that you are only interested in
  68.   '              the text that follows a delimiter. For example, as in a
  69.   '              command line string, you may have a string passed into your
  70.   '              Program that looks like Text$=" /n /o /d". The parser would
  71.   '              return 3 items in the array Parms$(1)="n", (2)="o", (3)="d".
  72.   '              All text up to the first delimiter is ignored.
  73.  
  74.   '    Type 2   A type 2 delimiter means that text is separated by the
  75.   '             delimiter, an all values on both sides of the delimiters
  76.   '             are returned. For example: If you have a string that is to be
  77.   '             broken apart based on where a Chr$(13) char is inserted, it
  78.   '             may look like this: Text$="This is"+Chr$(13)+"a sample."
  79.   '             The parser would return 2 items in the array.
  80.   '             Parms$(1)="This is "   and    Parms$(2)="a sample."
  81.  
  82.   '             Given these examples, it should be very easy to create other
  83.   '             types of delimiters. For example, it might be desirable to
  84.   '             have a delimiter type that is at the end of a string section.
  85.   '             This would be the opposite of a Type 1, such that any text
  86.   '             after the last delimiter would be ignored. Example:
  87.  
  88.   '             Text$ = "This is| a sample| text line."
  89.   '             Call Parser$(Text$, "|", Parms$(), 3)   ' type 3
  90.   '             This would return 2 items; Parms$(1)="This is", (2)="a sample"
  91.   '========================================================================
  92.   Local L%, T%, C%
  93.  
  94.   C%=0                                        ' item counter
  95.  
  96.   Select Case Flag%
  97.     Case 1            ' delimiter precedes items being returned
  98.       ' Example  Del="/"  String$="/m /no /yes"
  99.       ' returns 3 items
  100.  
  101.       T% = Tally(Text$, ANY Delimiter$)       ' number of delimiters found
  102.  
  103.       If T% = 0 then                          ' no delimiters
  104.         ReDim Parms$(0)                       ' erase array values
  105.         Exit Select                           ' exit sub
  106.       End If
  107.  
  108.       ReDim Parms$(1: T%)                     ' create array to hold items
  109.  
  110.       DO
  111.         Incr C%                               ' increment counter
  112.         L% = Instr(Text$, ANY Delimiter$)     ' pointer for first delimiter
  113.         Text$ = Right$(Text$, Len(Text$)-L%)  ' ignore all up to first delim
  114.                                               ' since the delimiter precedes.
  115.  
  116.         L% = Instr(Text$, ANY Delimiter$)     ' pointer for next delimiter
  117.  
  118.         If L% <> 0 then
  119.           Parms$(C%) = Left$(Text$, L%-1)     ' get all up to next delim.
  120.         Else
  121.           Parms$(C%) = Text$                  ' get all remaining
  122.         End If
  123.  
  124.         Text$ = Right$(Text$, Len(Text$)-Len(Parms$(C%)))
  125.       LOOP While Len(Text$)
  126.  
  127.     Case 2            ' delimiter separates items being returned
  128.       ' Example  Del=Chr$(13)  String$="This is"+Chr$(13)+" a test".
  129.       ' returns 2 items
  130.  
  131.       T% = Tally(Text$, ANY Delimiter$)       ' number of delimiters found
  132.  
  133.       If T% = 0 then                          ' no delimiters
  134.         ReDim Parms$(0)                       ' erase array values
  135.         Exit Select                           ' exit sub
  136.       End If
  137.  
  138.       ReDim Parms$(1: T%+1)                   ' create array to hold items
  139.  
  140.       DO
  141.         Incr C%                               ' increment counter
  142.         L% = Instr(Text$, ANY Delimiter$)     ' pointer for delimiter
  143.  
  144.         If L% <> 0 then
  145.           Parms$(C%) = Left$(Text$, L%-1)     ' get all up to delimiter
  146.           Text$ = Right$(Text$, Len(Text$) - Len(Parms$(C%))-1)
  147.         Else
  148.           Parms$(C%) = Text$                  ' get all remaining
  149.           Text$      = ""
  150.         End If
  151.       LOOP While Len(Text$)
  152.  
  153.     Case Else                                 ' some other flag
  154.       Parms$(1) = Text$                       ' Bad flag
  155.   End Select
  156.  
  157. End Sub
  158.